#define _CRT_SECURE_NO_WARNINGS 1
#include"binarytree.h"

BTNode* BTBuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	node->left = NULL;
	node->right = NULL;
	node->data = x;
}

BTNode* BTCreat()
{
	BTNode* node1 = BTBuyNode(1);
	BTNode* node2 = BTBuyNode(2);
	BTNode* node3 = BTBuyNode(3);
	BTNode* node4 = BTBuyNode(4);
	BTNode* node5 = BTBuyNode(5);
	BTNode* node6 = BTBuyNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;

	return node1;
}



void BTDestroy(BTNode* root)
{
	if (root == NULL)
		return;
	
	BTDestroy(root->left);
	BTDestroy(root->right);
	free(root);
}


void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	printf("%d ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);

}
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}


void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

int BTSize(BTNode* root)
{
	return root == NULL ? 0 : BTSize(root->left) + BTSize(root->right) + 1;
}
int BTHeight(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}

	int lefttree = BTHeight(root->left);
	int righttree = BTHeight(root->right);
	return lefttree > righttree ? lefttree + 1 : righttree + 1 ;

}
int BTKLevel(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}

	return BTKLevel(root->left, k - 1) + BTKLevel(root->right, k - 1);
}